package com.hb.game.pk.ui.activity.mine.recommend;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hb.game.pk.APP;
import com.hb.game.pk.R;
import com.hb.game.pk.adpter.RecommendListListAdapter;
import com.hb.game.pk.api.Host;
import com.hb.game.pk.api.RedBag;
import com.hb.game.pk.base.BaseActivity;
import com.hb.game.pk.beans.TJBean;
import com.hb.game.pk.network.RetrofitCreateHelper;
import com.hb.game.pk.utils.ToastUtils;
import com.hb.game.pk.widgets.titlebar.TitleBar;

import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RecommendListActivity extends BaseActivity<TJBean> {
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    int counts = 1;
    Call<TJBean> call;
    private List<TJBean.DataBeanX.DataBean> data;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_recommend_list;
    }

    @Override
    protected int getRootViewId() {
        return R.id.layout_root;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return R.id.rv_list;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        titleBar.getLlLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RecommendListActivity.this.finish();
            }
        });
        titleBar.setTitle("一级团队");
    }

    @Override
    protected void onRefreshData() {
        super.onRefreshData();
        call = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).tjList(APP.uid, counts);
        call.enqueue(new Callback<TJBean>() {
            @Override
            public void onResponse(Call<TJBean> call, Response<TJBean> response) {
                TJBean body = response.body();
                if (body == null) {
                    ToastUtils.showToast("请求错误");
                    return;
                }
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(body.getInfo());
                    return;
                }
                data = body.getData().getData();
                titleBar.setRightText(body.getData().getCount() + "人");
                initAdapter();
            }

            @Override
            public void onFailure(Call<TJBean> call, Throwable t) {
                ToastUtils.showToast("网络请求错误");
            }
        });
    }

    @Override
    protected void initAdapter() {
        if (mAdapter == null) {
            mAdapter = new RecommendListListAdapter(R.layout.item_recommend_list_list);
            mAdapter.setNewData(data);
            mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    TJBean.DataBeanX.DataBean dataBean = data.get(position);
                    String user_id = dataBean.getUser_id();
                    Intent intent = new Intent();
                    intent.putExtra("user_id", user_id);
                    intent.putExtra("counts", 1);
                    startActivity(MyTeatActivity.class, intent);
                }
            });
        } else {
            mAdapter.setNewData(data);
        }
        super.initAdapter();
    }

    @Override
    public boolean isNeedLoadMore() {
        return false;
    }
}
